Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.

Feature/enhance connection to multiple ble devices #89

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

scheunemann
Copy link

@scheunemann scheunemann commented Jun 16, 2017

As described in #81 there are issues connecting multiple devices. In fact, shortly before the first connection attempt scanning is stopped adaptors/ble.js#88. Commenting it out (as mentioend in #81) yields "noble warnings" with the sphero's BLE adapter : noble warning: unknown peripheral xxxxxxxxxxxx

Problem

Some BLE adapters cannot connect to peripheral when in scanning mode. The following minimal noble script:

var noble = require('noble');
var os = require('os');

noble.on('stateChange', function(state) {
    console.log(state);
    if (state === 'poweredOn') {
        noble.startScanning();
    } else {
    }
});

noble.on('discover', function(peripheral) {

    var localName = peripheral.advertisement.localName;
    console.log("discover ", localName);

    if(localName !== undefined && localName !== "" && localName.substring(0, "BB-".length) === "BB-") {
        console.log("Connecting ", localName);

        peripheral.on('connect', function() {
            console.log(localName, 'connected');
        });

        peripheral.on('disconnect', function() {
            console.log(localName, 'disconnected');
        });

        // noble.stopScanning(); // Edit
        peripheral.connect(function(error) {
            if(error) {
                console.log(error);
                return;
            }
            console.log(localName, 'connected!'); 
            // noble.startScanning(); // Edit
        return;
        });
    }

});

... will cause the follwoing error messages:

  • Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)) : Error: Command disallowed
  • Intel Dual Band Wireless-AC 7260 (Intel Corporation Wireless 7260 (rev 73)) : Error: Connection Rejected due to Limited Resources (0xd)

See more at noble's pull#638 or issue#35

Solution

This pull request stops scanning before an attempt to connect, and, when conencted, initiate further scanning. Tested with both physical BLE adapters (mentioned above) and works reliable.

Tested with the following minimal scripts:

minimal script 1

var sphero = require("sphero");
var ollie1 = sphero("AA:BB:CC:DD:EE:FF");
var ollie = sphero("11:22:33:44:55:66");

ollie.connect(function() {
    console.log("Ollie connected");
    ollie.color("red");
});

ollie1.connect(function() {
    console.log("Ollie1 connected");
    ollie1.color("blue");
});

minimal script 2

Like the Conway's Game example.

var sphero = require("sphero");

var spheros = {
	Remote 	: sphero("AA:BB:CC:DD:EE:FF"),
	Bot		: sphero("11:22:33:44:55:66")
}

function main() {
	connect(spheros, function() {
    	     for (var name in spheros) {start(name);}
	});
}

function connect(orbs, callback) {
  var total = Object.keys(orbs).length,
      finished = 0;

  function done() {
    finished++;
    if (finished >= total) { callback(); }
  }

  for (var name in orbs) {
   orbs[name].connect(done);
  }
}

function start(name) {
  var orb = spheros[name];
  orb.randomColor();
}

main();

Issue

I have only access to the two BLE devices mentioned above, no device present which may have worked anyway. Needs further testing.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant